package com.snail.billing.session;

import org.json.JSONException;
import org.json.JSONObject;

import com.snail.billing.DataCache;
import com.snail.billing.data.UUIDManager;
import com.snail.billing.json.JsonBase;
import com.snail.billing.net.BillingArrayHttpSession;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.net.BillingSecurity;

public class BindEmailSession extends BillingArrayHttpSession {
	
	private String type;

	public BindEmailSession(String type, String email, String validateCode) {
		DataCache cache = DataCache.getInstance();
		String host = cache.importParams.hostBusiness;
		String address = String.format("http://%s/passport/login/emailAuth.do", host);
		setAddress(address);
		addBillingPair("sessionId", BillingEncode.enCode(DataCache.getInstance().importParams.sessionId, "GBK"));
		addBillingPair("uuid", BillingEncode.enCode(UUIDManager.getUUID(), "GBK"));
		addBillingPair("aid", cache.importParams.aid);
		addBillingPair("gameId", cache.importParams.gameId);
		addBillingPair("type", type);
		this.type = type;
		addBillingPair("email", email);
		addBillingPair("loginName", BillingEncode.enCode(DataCache.getInstance().importParams.account, "GBK"));
		if (validateCode != null) {
			addBillingPair("validateCode", validateCode);
		}
		buildParamPair();
	}
	
	public String getType() {
		return type;
	}

	@Override
	public BillingSecurity getSecurity() {
		if (DataCache.getInstance().isSandbox) {
			return BillingSecurity.securitySandbox2;
		}
		return BillingSecurity.security;
	}
	
	public static class JsonData extends JsonBase {

		private String sessionId;
		private String account;

		public JsonData(String jsonString) {
			super(jsonString);
			try {
				if (getCode() == 1) {
					JSONObject json = new JSONObject(jsonString);
					if (json.has("sessionId")) {
						sessionId = json.getString("sessionId");
					}
					if (json.has("account")) {
						account = json.getString("account");
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		public String getSessionId() {
			return sessionId;
		}

		public String getAccount() {
			return account;
		}

	}


}
